/**
 * Created by zyx on 2017/9/18.
 */
angular.module('service.auth', [
        'ngResource',
        'ngCookies'
    ])
    .service('AuthService', AuthService);
AuthService.$inject = [ '$resource', '$localStorage'];
function AuthService( $resource, $localStorage) {

    var userid = null;
    var username = null;

    // login（统一登录操作）
    this.login = function (user, success, error) {
        var resource = $resource(authurl + 'login', {}, {
            login: {
                method: 'POST',
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                transformRequest: function (data) {
                    var str = [];
                    for (var d in data) {
                        str.push(encodeURIComponent(d) + "=" + encodeURIComponent(data[d]));
                    }
                    return str.join("&");
                }
            }
        });
        //
        var loginuser = {
            "username": user.username,
            "password": hex_hmac_md5("fjswxxjsyxgstyrz", user.password),
            "acctype": 6
        };
        //
        var login_ret = resource.login({}, loginuser, success, error);
        login_ret.$promise.then(function (userinfo) {
            userid = userinfo.userid;
            username = userinfo.username;
        });
        return login_ret;
    };

    // 通过手机号判断用户是否存在
    this.getExit = function (phone) {
        var resource = $resource(authurl + "user/getinfo/byphone/:phone");
        return resource.get({phone: phone});
    };

    // 通过手机号获取验证码
    this.getCode = function (phone) {
        var resource = $resource(authurl + "user/reset/registcode/:phone");
        return resource.get({phone: phone});
    };

    // 手机号注册
    this.register = function (info) {
        var registeruser = {
            "username": info.username,
            "password": hex_hmac_md5("fjswxxjsyxgstyrz", info.password),
            "phone": info.phone,
            "code": info.code
        };
        var resource = $resource(authurl + "user/openregister");
        return resource.save(registeruser);
    };

    // 通过手机号获取忘记密码页面的验证码
    this.getFgPwdCode = function (phone) {
        var resource = $resource(authurl + "user/reset/applycode/:phone");
        return resource.get({phone: phone});
    };

    // 手机号忘记密码
    this.resetPwd = function (info) {
        var updateUser = {
            // "password": hex_hmac_md5("fjswxxjsyxgstyrz", info.password),
            "password": info.password,
            "phone": info.phone,
            "code": info.code
        };
        var resource = $resource(authurl + "user/reset/bycode/:phone");
        return resource.get({phone: updateUser.phone, code: updateUser.code, password: updateUser.password});
    };

    // 判断是否有店铺缓存
    this.isAuthenticated = function () {
        if (getObject(environmental + "shopid")) {
            return !!getObject(environmental + "shopid");
        } else {
            return false;
        }
    };

    // 读取localStorage对象
    var getObject = function (name) {
        if ($localStorage[name]) {
            return $localStorage[name];
        }
        return false;
    };

    this.getMerchantId = function (userid, succF, errF) {
        var resource = $resource(merchanturl + "queryinfo/:userid");
        return resource.get({userid: userid}, succF, errF);
    }
}